
DEFINE VARIABLE avgprice AS DECIMAL.
DEFINE VARIABLE part AS CHARACTER.

ASSIGN part = "150-0205620".

RUN comp_avg_cost (INPUT "RMB", INPUT "", INPUT 04/01/04,INPUT 04/30/04,INPUT part, OUTPUT avgprice).

DISP part FORMAT "X(12)"
     avgprice FORMAT ">>>>>9.9<<".

/*create the exchange rate of currency */
PROCEDURE comp_avg_cost.
    
    DEFINE INPUT PARAMETER base_curr  AS CHARACTER.
    DEFINE INPUT PARAMETER site  AS CHARACTER.
    DEFINE INPUT PARAMETER s_date AS DATE.
    DEFINE INPUT PARAMETER e_date AS DATE.
    DEFINE INPUT PARAMETER part   AS CHARACTER.
    DEFINE OUTPUT PARAMETER cst_avg AS DECIMAL.
    
    DEFINE VARIABLE um_rate AS DECIMAL.
    DEFINE VARIABLE exc_rate AS DECIMAL.
    DEFINE VARIABLE qty_tot AS DECIMAL.
    DEFINE VARIABLE qty_chg AS DECIMAL.
    DEFINE VARIABLE cst_tot AS DECIMAL.
    
    FOR EACH pt_mstr NO-LOCK WHERE pt_part BEGINS part : 
        
        /*compute the average cost*/
        qty_tot = 0.
        cst_tot = 0.
        FOR EACH tr_hist WHERE tr_type = 'rct-po' AND tr_part = pt_part
                           AND (s_date <= tr_date AND tr_date <= e_date) 
                           AND tr_site BEGINS site 
                           AND tr_addr <> "S15032" NO-LOCK /*Filter the vend S13052 */
            BREAK BY tr_part BY tr_addr:

            RUN getItemConvUm(INPUT tr_part,INPUT pt_um, INPUT tr_um, OUTPUT um_rate).

            IF base_curr = tr_curr THEN
               exc_rate = 1.0.
            ELSE DO:
               RUN getCurrRate(INPUT base_curr, INPUT tr_curr, INPUT s_date, INPUT e_date, OUTPUT exc_rate).
               exc_rate = exc_rate * 1.17. /*if the currency is not base so the price is not tax_in */
            END.

            /*accept the receiver in 500 RW03 warehouse N-N-N*/
            IF CAN-DO ("M,S",tr_ship_type) OR tr_status = "N-N-N" THEN DO:
                qty_tot = qty_tot + tr_qty_loc * um_rate.
                cst_tot = cst_tot + tr_qty_loc * um_rate * tr_price * exc_rate.
            END.
            ELSE DO:
                qty_tot = qty_tot + tr_qty_chg * um_rate.
                cst_tot = cst_tot + tr_qty_chg * um_rate * tr_price * exc_rate.
            END.

 DISP tr_part tr_addr tr_curr tr_qty_loc um_rate qty_tot cst_tot. 
     
        END.

        IF qty_tot <> 0 THEN
           ASSIGN cst_avg = cst_tot / qty_tot.
        ELSE 
           ASSIGN cst_avg = ?.

    END.
 /*OUTPUT CLOSE. */

END PROCEDURE.


PROCEDURE getCurrRate.
    DEFINE INPUT PARAMETER base_curr  AS CHARACTER.
    DEFINE INPUT PARAMETER targ_curr  AS CHARACTER.
    DEFINE INPUT PARAMETER s_date AS DATE.
    DEFINE INPUT PARAMETER e_date AS DATE.
    DEFINE OUTPUT PARAMETER exc_rate AS DECIMAL.
    
    FOR EACH exr_rate WHERE (exr_start_date <= e_date AND e_date <= exr_end_date)  OR e_date = ? NO-LOCK:

        IF exr_curr1 = base_curr AND exr_curr2 = targ_curr THEN DO:
           exc_rate = exr_rate.
           RETURN.
        END.
        ELSE IF exr_curr2 = base_curr AND exr_curr1 = targ_curr THEN DO:
           exc_rate = exr_rate2.
           RETURN.
        END.
    END.

END PROCEDURE.

PROCEDURE getItemConvUm.
    DEFINE INPUT  PARAMETER part AS CHARACTER.
    DEFINE INPUT  PARAMETER um1 AS CHARACTER.   /*stock base unit */
    DEFINE INPUT  PARAMETER um2 AS CHARACTER.   /*conver unit*/
    DEFINE OUTPUT PARAMETER rate AS DECIMAL.

    FIND FIRST um_mstr NO-LOCK WHERE um_part = part AND um_um = um1 AND um_alt_um = um2 NO-ERROR .
    IF AVAILABLE um_mstr THEN
       rate = um_conv .
    ELSE DO:
       FIND FIRST um_mstr NO-LOCK WHERE um_um = um1 AND um_alt_um = um2 NO-ERROR .
       IF AVAILABLE um_mstr THEN
          rate = um_conv .
       ELSE 
          rate = 1 .
    END.
END PROCEDURE.

